package global

import (
	"embed"
	"fmt"
	"gitee.com/cdsailyj/go-vpn/common/core"
	"os"
	"path/filepath"
)

//go:embed  config/*
var configFile embed.FS

// GetConfigData 获取嵌入文件的内容
func GetConfigData() ([]byte, error) {
	return configFile.ReadFile(fmt.Sprintf("config/config.%s.yaml", AppEnv))
}

// LoadConfig 加载配置项
func LoadConfig() {
	b, _ := GetConfigData()
	Viper = core.Viper(&CONFIG, b)
}

func SearchDir() []string {
	exePath, _ := os.Executable()
	pwd, _ := os.Getwd()
	return []string{pwd, filepath.Dir(exePath)}
}

// FindConfigFile 扫描需要的配置或者模板
func FindConfigFile(env string) string {
	searchDir := SearchDir()
	name := fmt.Sprintf("config.%s.yaml", env)
	for _, dir := range searchDir {
		filePath := filepath.Join(dir, name)
		if _, err := os.Stat(filePath); err == nil {
			return filePath
		}
	}
	return ""
}
